//
//  STLoginViewController.m
//  BasicAnimation-OC
//
//  Created by Stan on 2017-02-14.
//  Copyright © 2017 stan. All rights reserved.
//

#import "STLoginViewController.h"

@interface STLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberTextField;
@property (weak, nonatomic) IBOutlet UITextField *pinTextField;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

//    电话号码文本框的中心点约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *phoneNumberCenterX;
//    密码文本框的中心点约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pinCenterX;
//    登录按钮中心点的约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *loginBtnCenterX;


@end

@implementation STLoginViewController

//    视图将要显示出来的方法
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    //        修改电话号码、密码、登录按钮中心点的约束，为动画做准备
    self.phoneNumberCenterX.constant -= self.view.bounds.size.width;
    self.pinCenterX.constant -= self.view.bounds.size.width;
    self.loginBtnCenterX.constant -= self.view.bounds.size.width;
    
    //        隐藏登录按钮
    self.loginBtn.alpha = 0;
    
}


//    视图已经显示
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //        将登录按钮显示出来
    self.loginBtn.alpha = 1;
    
    __weak typeof(self) weakSelf = self;

    
    //        设置电话号码框移入的动画
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        weakSelf.phoneNumberCenterX.constant += weakSelf.view.bounds.size.width;
        [weakSelf.view layoutIfNeeded];
    } completion:nil];
    
    //        设置密码框移入的动画，为了有节奏，要稍微比电话号码晚一点点出现。delay设置为0.1
    [UIView animateWithDuration:0.5 delay:0.1 options:UIViewAnimationOptionCurveEaseOut animations:^{
        weakSelf.pinCenterX.constant += weakSelf.view.bounds.size.width;
        [weakSelf.view layoutIfNeeded];
    } completion:nil];
    
    //        登录按钮的动画
    [UIView animateWithDuration:0.5 delay:0.2 options:UIViewAnimationOptionCurveEaseOut animations:^{
        weakSelf.loginBtnCenterX.constant += weakSelf.view.bounds.size.width;
        [weakSelf.view layoutIfNeeded];
    } completion:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    __weak typeof(self) weakSelf = self;

    //        设置边框圆角
    self.phoneNumberTextField.layer.cornerRadius = 5;
    self.pinTextField.layer.cornerRadius = 5;
    self.loginBtn.layer.cornerRadius = 5;

    

}


//    设置电池栏的状态为白色
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

//    登录按钮点击事件
- (IBAction)loginBtnClick:(id)sender {
    
    __weak typeof(self) weakSelf = self;

            //        这里模拟的是登录失败的状况。设置一个阻尼动画，失败的时候登录按钮进行晃动
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:20 options:UIViewAnimationOptionCurveLinear animations:^{
        weakSelf.loginBtn.bounds = CGRectMake(weakSelf.loginBtn.bounds.origin.x - 20, weakSelf.loginBtn.bounds.origin.y, weakSelf.loginBtn.bounds.size.width, weakSelf.loginBtn.bounds.size.height);
        //            在动画没有播放完成之前，关闭按钮的交互
        [weakSelf.loginBtn setEnabled:NO];
        
    } completion:^(BOOL finished) {
        //            动画播放完成后，重新开启按钮的交互
        [weakSelf.loginBtn setEnabled:YES];
        //            把登录按钮的位置进行复原
        weakSelf.loginBtn.bounds = CGRectMake(weakSelf.loginBtn.bounds.origin.x + 20, weakSelf.loginBtn.bounds.origin.y, weakSelf.loginBtn.bounds.size.width, weakSelf.loginBtn.bounds.size.height);
    }];
}

//    返回按钮的点击事件
- (IBAction)backBtnClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


@end
